# Replication Archive for: Aggarwal, Minali, Jennifer Allen, Alex Coppock, 
# Dan Frankowski, Sol Messing, Kelly Zhang, James Barnes, Andrew Beasley, 
# Harry Hantman, and Sylvan Zheng: The impact of digital campaign advertising 
# during the 2020 US presidential election: evidence from survey experiments, 
# field experiments, and a campaign-level experiment. 
# Nature Human Behavior, forthcoming.

library(tidyverse)
library(lubridate)
library(viridis)
library(patchwork)

dates_field <- read_rds("fielding_dates.rds")
dates_survey <- read_rds("fielding_dates_survey.rds")
ad_descriptives <- read_csv("treatment_descriptives.csv")


dates <-
  bind_rows(dates_field, dates_survey) %>%
  mutate(survey_name = str_extract(survey_name, "STG\\d+")) %>%
  filter(!is.na(survey_name))

ad_descriptives <-
  ad_descriptives %>%
  mutate(experiment = str_extract(experiment, "STG\\d+"))

joined <-
  dates %>%
  inner_join(ad_descriptives,
             by = c("survey_name" = "experiment")) %>%
  filter((is_biden | is_trump_neg | is_harris) & !is_turnout,
         date < "2020-09-01") %>%
  mutate(
    ad_type = case_when(
      ((is_biden | is_harris) & !is_trump_neg) ~ "Promotional",
      (is_trump_neg & (!is_biden & !is_harris)) ~ "Attack",
      ((is_biden | is_harris) & is_trump_neg) ~ "Contrast",
      TRUE ~ NA_character_
    ),
    month = as.Date(floor_date(date, unit = "month")),
    ad_format_coarse =
      case_when(
        ad_format %in% c("Articles", 
                         "Video + Articles", 
                         "Video + Graphic + Articles",
                         "Graphics + Articles") ~ "Promoted news",
        ad_format %in% c("Video") ~ "Traditional video",
        TRUE ~ "Other",
      )
  )

#table(joined$ad_format)

gg_df_1 <-
  joined %>%
  group_by(ad_type, month) %>%
  summarize(n = n()) %>%
  rename(`Ad Type` = ad_type)

g1 <- 
  ggplot(gg_df_1, aes(
    x = month,
    y = n,
    group = `Ad Type`,
    color = `Ad Type`,
    shape = `Ad Type`
  )) +
  geom_line() +
  geom_point() +
  ylab("Number of Ads Tested") +
  theme_bw() +
  theme(
    axis.text.x = element_text(angle = 60, hjust = 1),
    axis.title.x = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "bottom"
  ) +
  scale_color_brewer(palette = "Set1") + 
  scale_x_date(labels = scales::date_format(format = "%B %Y")) +
  scale_y_continuous(limits = c(0, 15)) +
  scale_fill_brewer(palette = "Spectral") 

gg_df_2 <-
  joined %>%
  group_by(ad_format_coarse, month) %>%
  summarize(n = n()) %>%
  rename(`Ad Format` = ad_format_coarse) 

  
g2 <-
  ggplot(gg_df_2, aes(x = month, y = n, color  = `Ad Format`,
                      shape  = `Ad Format`)) +
  geom_line() +
  geom_point() +
  ylab("Number of Ads Tested") +
  theme_bw() +
  theme(
    axis.text.x = element_text(angle = 60, hjust = 1),
    axis.title.x = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "bottom"
  ) +
  scale_color_brewer(palette = "Set2") +
  scale_x_date(date_labels =  "%B %Y") +
  scale_y_continuous(limits = c(0, 15)) +
  scale_fill_brewer(palette = "Spectral")

g <- g1 + g2

#ggsave("output/figure_B1.pdf", g, height = 4, width = 8)
